package com.example.myapplication.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.BaseOnclickListener;
import com.example.myapplication.PlayActivity;
import com.example.myapplication.R;
import com.example.myapplication.adapter.ResultSongAdapter;
import com.example.myapplication.api.MusicApi;
import com.example.myapplication.api.SongListApi;
import com.example.myapplication.entity.BaseResponse;
import com.example.myapplication.entity.BaseResponseSongUrl;
import com.example.myapplication.entity.Data;
import com.example.myapplication.entity.Hots;
import com.example.myapplication.entity.SearchResult;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.content.ContentValues.TAG;

public class ResultSongFragment extends Fragment {
    private RecyclerView recyclerView;
    private ResultSongAdapter adapter;
    private Context context;
    private Hots hots;
    private SearchResult result;
    private String keyword;
    private List<SearchResult.AlbumsBean> albums;
    private String url;
    private Data data;
    private int albumID;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return LayoutInflater.from(container.getContext()).inflate(R.layout.layout_recycle_view, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        context = getContext();

        recyclerView = view.findViewById(R.id.recycle_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        adapter = new ResultSongAdapter();
        recyclerView.setAdapter(adapter);

        Bundle bundle = getArguments();


        if (bundle.getString("search_input") != null){
            keyword = bundle.getString("search_input");
        }
        else {
            hots = bundle.getParcelable("search_key");
            keyword = hots.getSearchWord();
        }


        loadData(keyword);



        adapter.setBaseOnclickListener(new BaseOnclickListener() {
            @Override
            public void click(int position) {
                Intent intent = new Intent(context, PlayActivity.class);
                Bundle bundle1 = new Bundle();
                bundle1.putString("songID",result.getSongs().get(position).getId());
                bundle1.putString("songName", result.getSongs().get(position).getName());
                bundle1.putInt("albumID", result.getSongs().get(position).getAlbum().getId());
                bundle1.putString("singerName", result.getSongs().get(position).getArtists().get(0).getName());
                intent.putExtras(bundle1);
                startActivity(intent);
            }
        });
    }

    private void loadData(String keyword){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        MusicApi api = retrofit.create(MusicApi.class);

        Call<BaseResponse> call = api.getResultSong(keyword);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                BaseResponse baseResponse = response.body();
                if (baseResponse.isSuccess()){
                    result = baseResponse.getResult();
                    adapter.setData(result);
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                t.printStackTrace();
                Log.e(TAG, "onFailure: ", t);
            }
        });
    }



    public static ResultSongFragment newInstance(Bundle bundle){
        ResultSongFragment fragment = new  ResultSongFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


}
